package net.mihome.app.ui.view;

import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import net.mihome.app.AppContext;
import net.mihome.app.AppException;
import net.mihome.app.adapter.ListViewNewsAdapter;
import net.mihome.app.bean.News;
import net.mihome.app.bean.NewsList;
import net.mihome.app.common.UIHelper;
import net.mihome.app.widget.PullToRefreshListView;
import net.mihome.app.R;

public class MihomeZixunView extends XListView {
	private Handler handler;
	private ListViewNewsAdapter adapter;
	private List<News> lvNewsData = new ArrayList<News>();
	private AppContext appContext;
	private int begin=0;
	private int num=10;
	private int newsTypeId=NewsList.CATALOG_ALL;
	private final static int PULL=9;
	private final static int PUSH=10;
	private MyViewLinster myViewLinster;
	public MihomeZixunView(Context context, AttributeSet attrs) {
		super(context, attrs);;
		afterInit();
		// TODO Auto-generated constructor stub
	}
	public MihomeZixunView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		afterInit();
		// TODO Auto-generated constructor stub
	}
	private void afterInit(){
		adapter = new ListViewNewsAdapter(getContext(), lvNewsData,R.layout.news_listitem);
		setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				News news = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					news = (News) view.getTag();
				} else {
					TextView tv = (TextView) view
							.findViewById(R.id.news_listitem_title);
					news = (News) tv.getTag();
				}
				if (news == null)
					return;
				// 跳转到新闻详情
				UIHelper.showNewsRedirect(view.getContext(), news);
			}
		});
		setPullLoadEnable(true);
		setPullRefreshEnable(true);
		setAdapter(adapter);
		appContext=(AppContext) getContext().getApplicationContext();
		handler=new Handler(new Handler.Callback() {
			@Override
			public boolean handleMessage(Message msg) {
				// TODO Auto-generated method stub
				NewsList newsList=(NewsList) msg.obj;
				if(newsList!=null&&newsList.getNewslist()!=null){
					switch (msg.what) {
					case PULL:
						lvNewsData.clear();
						lvNewsData.addAll(0,newsList.getNewslist());
						break;
					case PUSH:
						lvNewsData.addAll(newsList.getNewslist());
						break;
					default:
						break;
					}
				}
					adapter.notifyDataSetChanged();
					stopRefresh();
					stopLoadMore();
					setRefreshTime("不久前");

					if(myViewLinster!=null){
						myViewLinster.onLoadComplete();
					}
				return false;
			}
		});
		setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				new Thread(new Runnable() {
					@Override
					public void run() {
						Message msg=new Message();
						msg.what=PULL;
						try {
							begin=0;
							num=10;
							NewsList list=appContext.getNewsList2(newsTypeId, begin, num);
							if(list!=null&&!list.getNewslist().isEmpty()&&getVisibility()==View.VISIBLE){
//								lvNewsData.clear();
//								lvNewsData.addAll(0,list.getNewslist());
								msg.obj=list;
							}
						} catch (AppException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						handler.sendMessage(msg);
					}
				}).start();
			}
			
			@Override
			public void onLoadMore() {
				Message msg=new Message();
				try {
					begin=(begin+1)*num;
					msg.what=PUSH;
					NewsList list=appContext.getNewsList2(newsTypeId, begin, num);
					if(list!=null&&!list.getNewslist().isEmpty()&&getVisibility()==View.VISIBLE){
//						lvNewsData.addAll(list.getNewslist());
						msg.obj=list;
					}
				} catch (AppException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				handler.sendMessage(msg);
			}
		});
	}
	public void initData(int typeId){
		newsTypeId=typeId;
		if(myViewLinster!=null){
			myViewLinster.onBeforeLoad();
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message msgMessage=new Message();
				msgMessage.what=PULL;
				try {
					begin=0;
					num=10;
					NewsList list=appContext.getNewsList2(newsTypeId, begin, num);
					if(list!=null&&!list.getNewslist().isEmpty()&&getVisibility()==View.VISIBLE){
//						lvNewsData.clear();
//						lvNewsData.addAll(list.getNewslist());
						msgMessage.obj=list;
					}
				} catch (AppException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				handler.sendMessage(msgMessage);
			}
		}).start();
	}
	public MyViewLinster getMyViewLinster() {
		return myViewLinster;
	}
	public void setMyViewLinster(MyViewLinster myViewLinster) {
		this.myViewLinster = myViewLinster;
	}
	
}
